#!/bin/bash

set -eu

echo "Workspace root: ${WS_ROOT}"
cd ${WS_ROOT}/ros

export CONDA_ENV_ROOT=/root/conda/envs/ros_env
export CMAKE_COMMAND=${WS_ROOT}/ohos-robot-toolchain/linux/native/build-tools/cmake/bin/cmake

source /root/conda/etc/profile.d/conda.sh

conda activate ros_env

colcon build --merge-install --event-handlers console_cohesion+ \
    --cmake-args -DBUILD_TESTING=OFF -DCMAKE_VERBOSE_MAKEFILE=ON -Wno-dev -DCMAKE_TOOLCHAIN_FILE=${WS_ROOT}/ros/ohos.toolchain.docker.cmake


echo "Patching scripts and dynamic libraries..."

find ./install -type f | while read -r file; do
    if file "$file" | grep -q 'text'; then
        sed -i "s|${CONDA_ENV_ROOT}/bin/python3.12|/data/python-wheel-3.12/bin/python3|g" "$file"
        sed -i "s|${CONDA_ENV_ROOT}/bin/python3|/data/python-wheel-3.12/bin/python3|g" "$file"
    fi
done
find ./install/lib/python3.12/site-packages -type f -name "*cpython-312-x86_64-linux-gnu.so" | while read file; do
    echo "Renaming: $file -> ${file//cpython-312-x86_64-linux-gnu/cpython-312}"
    mv "$file" "${file//cpython-312-x86_64-linux-gnu/cpython-312}"
done

echo "Copying dependencies..."

cp ../ohos-robot-toolchain/linux/native/llvm/lib/aarch64-linux-ohos/libc++_shared.so ./install/lib/

SYSROOT=../ohos-robot-toolchain/linux/native/sysroot/usr/lib
cp ${SYSROOT}/libtinyxml2.so.9 ./install/lib/
cp ${SYSROOT}/libssl.so.3 ./install/lib/
cp ${SYSROOT}/libcrypto.so.3 ./install/lib/
cp ${SYSROOT}/libacl.so.1 ./install/lib/
cp ${SYSROOT}/libattr.so.1 ./install/lib/

cp ${SYSROOT}/libGL.so.1 ./install/lib/
cp ${SYSROOT}/libGLU.so.1 ./install/lib/
cp ${SYSROOT}/libSM.so.6 ./install/lib/
cp ${SYSROOT}/libICE.so.6 ./install/lib/
cp ${SYSROOT}/libX11.so.6 ./install/lib/
cp ${SYSROOT}/libXext.so.6 ./install/lib/
cp ${SYSROOT}/libXt.so.6 ./install/lib/
cp ${SYSROOT}/libXrandr.so.2 ./install/lib/
cp ${SYSROOT}/libXaw7.so.7 ./install/lib/libXaw.so.7
cp ${SYSROOT}/libQt5Widgets.so ./install/lib/
cp ${SYSROOT}/libQt5Gui.so ./install/lib/
cp ${SYSROOT}/libQt5Core.so ./install/lib/
cp ${SYSROOT}/libcurl.so.4 ./install/lib/

cp ${SYSROOT}/libglapi.so.0 ./install/lib/
cp ${SYSROOT}/libgallium-24.3.4.so ./install/lib/
cp ${SYSROOT}/libdrm.so.2 ./install/lib/
cp ${SYSROOT}/libxcb-glx.so.0 ./install/lib/
cp ${SYSROOT}/libxcb.so.1 ./install/lib/
cp ${SYSROOT}/libX11-xcb.so.1 ./install/lib/

cp ${SYSROOT}/libXxf86vm.so.1 ./install/lib/
cp ${SYSROOT}/libxcb-shm.so.0 ./install/lib/
cp ${SYSROOT}/libexpat.so.1 ./install/lib/
cp ${SYSROOT}/libxcb-dri3.so.0 ./install/lib/
cp ${SYSROOT}/libxcb-present.so.0 ./install/lib/
cp ${SYSROOT}/libxcb-xfixes.so.0 ./install/lib/
cp ${SYSROOT}/libXau.so.6 ./install/lib/
cp ${SYSROOT}/libXrender.so.1 ./install/lib/
cp ${SYSROOT}/libzstd.so.1 ./install/lib/

cp ${SYSROOT}/libxcb-sync.so.1 ./install/lib/
cp ${SYSROOT}/libxshmfence.so.1 ./install/lib/
cp ${SYSROOT}/libXmu.so.6 ./install/lib/
cp ${SYSROOT}/libXpm.so.4 ./install/lib/
cp ${SYSROOT}/libomp.so ./install/lib/

cp ../version/netifaces.so ./install/lib/python3.12/site-packages/
cp -r ${CONDA_ENV_ROOT}/lib/python3.12/site-packages/lark ./install/lib/python3.12/site-packages/

TIMESTAMP=$(date +"%Y%m%d%H%M%S")
OUTPUT_NAME=ohos-humble-build-${TIMESTAMP}.tar.gz
tar -zcpvf ${OUTPUT_NAME} ./install

mv ${OUTPUT_NAME} ${WS_ROOT}

echo "Finish building!"
